package com.xb.loan.encryptor.util;


import com.xb.loan.encryptor.bean.LotteryBean;
import com.xb.loan.encryptor.bean.request.other.AddPartnerPubKeyReq;
import com.xb.loan.encryptor.bean.request.other.GeneratePartnerKeyReq;
import com.xb.loan.encryptor.bean.request.other.QueryPartnerKeyReq;
import com.xb.loan.encryptor.bean.response.other.GeneratePartnerKeyRep;
import com.xb.loan.encryptor.bean.response.other.QueryPartnerKeyRep;
import com.xb.loan.encryptor.classloader.ClassLoaderConfig;
import com.xb.loan.encryptor.comm.BasicSocketManager;
import com.xb.loan.util.DateHelper;

import java.util.Date;

public class OssCryptHelper {
	
	// 添加合作商密钥
	public static String genPartnerKey(String partnerId){
		
		GeneratePartnerKeyReq req = new GeneratePartnerKeyReq();
		
		req.setHead(LotteryBean.createHead("105", ClassLoaderConfig.getConfig("crypt").getGuid()));
		req.setPartnerId(partnerId);
		
		GeneratePartnerKeyRep rep = BasicSocketManager.send(req);
		
		return rep.getPublicKey();
	}
	
	// 查询合作商密钥
	public static String queryPartnerKey(String keyType, String partnerId){
		
		QueryPartnerKeyReq req = new QueryPartnerKeyReq();
		
		req.setHead(LotteryBean.createHead("103", ClassLoaderConfig.getConfig("crypt").getGuid()));
		req.setKeyType(keyType);
		req.setPartnerId(partnerId);
		req.setKeyTime(DateHelper.formatDate("yyyyMMddHHmmss", new Date()));
		
		QueryPartnerKeyRep rep = BasicSocketManager.send(req);
		
		return rep.getPartnerKey();
	}
	
	// 添加合作商公钥
	public static void addPartnerPubKey(String partnerId, String partnerPubKey){
		
		AddPartnerPubKeyReq req = new AddPartnerPubKeyReq();
		
		req.setHead(LotteryBean.createHead("106", ClassLoaderConfig.getConfig("crypt").getGuid()));
		req.setPartnerId(partnerId);
		req.setPubKey(partnerPubKey);
		
		System.out.println(req);
		
		BasicSocketManager.send(req);
	}
	
	public static void main(String[] args) {
		
		genPartnerKey("10000000345");
	}
	
}
